// @Author : zaniu(zzaniu@126.com)
// @Time   : 2021/9/6 20:39
// @Desc   :
//
//                   _ooOoo_
//                  o8888888o
//                  88" . "88
//                  (| -_- |)
//                  O\  =  /O
//               ____/`---'\____
//             .'  \\|     |//  `.
//            /  \\|||  :  |||//  \
//           /  _||||| -:- |||||-  \
//           |   | \\\  -  /// |   |
//           | \_|  ''\---/''  |   |
//           \  .-\__  `-`  ___/-. /
//         ___`. .'  /--.--\  `. . __
//      ."" '<  `.___\_<|>_/___.'  >'"".
//     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//     \  \ `-.   \_ __\ /__ _/   .-` /  /
// =====`-.____`-.___\_____/___.-`____.-'=====
//                   `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//           佛祖保佑       永无BUG
//
package nacos

import (
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/model"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

func Register(ipHost string, port uint64, serviceName string) (bool, error) {
	namingClient, err := getNewNamingClient()
	if err != nil {
		fmt.Println("getNewNamingClient error, err = ", err.Error())
		return false, err
	}
	ok, err := namingClient.RegisterInstance(vo.RegisterInstanceParam{
		Ip:          ipHost,
		Port:        port,
		ServiceName: serviceName,
		Weight:      10,
		Enable:      true,
		Healthy:     true,
		Ephemeral:   true,
	})
	if err != nil {
		fmt.Println("注册失败, err = ", err.Error())
		return false, err
	}
	return ok, nil
}

func Deregister(ipHost string, port uint64, serviceName string) (bool, error) {
	namingClient, err := getNewNamingClient()
	if err != nil {
		fmt.Println("getNewNamingClient error, err = ", err.Error())
		return false, err
	}
	ok, err := namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
		Ip:          ipHost,
		Port:        port,
		ServiceName: serviceName,
		Ephemeral:   true,
	})
	if err != nil {
		fmt.Println("注销失败, err = ", err.Error())
		return false, err
	}
	return ok, nil
}

func SelectOneHealthyInstance(serviceName, group, cluster string) (*model.Instance, error) {

	namingClient, err := getNewNamingClient()
	if err != nil {
		fmt.Println("getNewNamingClient error, err = ", err.Error())
		return new(model.Instance), err
	}
	if len(group) == 0 {
		group = "DEFAULT_GROUP"
	}
	if len(cluster) == 0 {
		cluster = "DEFAULT"
	}
	instance, err := namingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
		ServiceName: serviceName,
		GroupName:   group,             // 默认值DEFAULT_GROUP
		Clusters:    []string{cluster}, // 默认值DEFAULT
	})
	fmt.Println(instance)
	return instance, nil
}

// 可以用来做缓存啊, 上线下线都会调用 SubscribeCallback, 然后直接刷新本地缓存(缓存: ip+port)
func SubscribeServer(serviceName, group, cluster string, callback func([]model.SubscribeService, error)) error {
	namingClient, err := getNewNamingClient()
	if err != nil {
		fmt.Println("clients.NewNamingClient error, err = ", err.Error())
		return err
	}
	if len(group) == 0 {
		group = "DEFAULT_GROUP"
	}
	if len(cluster) == 0 {
		cluster = "DEFAULT"
	}
	return namingClient.Subscribe(&vo.SubscribeParam{
		ServiceName:       serviceName,
		GroupName:         group,             // 默认值DEFAULT_GROUP
		Clusters:          []string{cluster}, // 默认值DEFAULT
		SubscribeCallback: callback,
	})
}

func Unsubscribe(serviceName, group, cluster string, callback func([]model.SubscribeService, error)) error {
	namingClient, err := getNewNamingClient()
	if err != nil {
		fmt.Println("clients.NewNamingClient error, err = ", err.Error())
		return err
	}
	if len(group) == 0 {
		group = "DEFAULT_GROUP"
	}
	if len(cluster) == 0 {
		cluster = "DEFAULT"
	}
	return namingClient.Unsubscribe(&vo.SubscribeParam{
		ServiceName:       serviceName,
		GroupName:         group,             // 默认值DEFAULT_GROUP
		Clusters:          []string{cluster}, // 默认值DEFAULT
		SubscribeCallback: callback,
	})
}
